package com.jvtd.pay;

import android.app.Activity;
import android.support.annotation.IntDef;
import android.text.TextUtils;

import com.jvtd.alipay.JvtdAliPay;
import com.jvtd.wxpay.JvtdWxPay;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class JvtdPay {
    public static final int PAY_WECHAT = 1;
    public static final int PAY_ALI = 2;
    @IntDef({PAY_WECHAT, PAY_ALI})
    @Retention(RetentionPolicy.SOURCE)
    public @interface PayType {
    }

    public static final int ERROR_PAY = 1;  //支付失败
    //支付宝错误类型
    public static final int ERROR_RESULT = 2;   //支付结果解析错误
    public static final int ERROR_NETWORK = 3;  //网络连接错误//

    //微信错误类型
    public static final int ERROR_WX_NO_OR_LOW_WX = 4;   //未安装微信或微信版本过低
    public static final int ERROR_PAY_PARAM = 5;  //支付参数错误 //微信错误

    @IntDef({ERROR_PAY, ERROR_RESULT, ERROR_NETWORK,ERROR_WX_NO_OR_LOW_WX,ERROR_PAY_PARAM})
    @Retention(RetentionPolicy.SOURCE)
    public @interface PayErrorCode {
    }

    private static volatile JvtdPay instance;
    private String mWxAppId = "";
    private @PayType int mPayType;

    public static JvtdPay getInstance() {
        if (instance == null)
            synchronized (JvtdPay.class) {
                if (instance == null)
                    instance = new JvtdPay();
            }
        return instance;
    }

    private JvtdPay() {
    }

    public interface PayResultCallBack {
        void onSuccess(@PayType int type); //支付成功

        void onDealing();    //正在处理中 小概率事件 此时以验证服务端异步通知结果为准  仅支付宝

        void onError(@PayType int type,@JvtdPay.PayErrorCode int error_code);   //支付失败

        void onCancel(@PayType int type);    //支付取消
    }

    /**
     * 微信配置
     *
     * @author Chenlei
     * created at 2018/9/28
     **/
    public JvtdPay init(String wx_appid){
        this.mWxAppId = wx_appid;
        return this;
    }

    public void doPay(Activity activity,PayBean payBean,PayResultCallBack payResultCallBack){
        if (payBean == null || payBean.getPayType() != PAY_WECHAT && payBean.getPayType() != PAY_ALI) {
            try {
                throw new Exception("支付类型错误");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
        if (TextUtils.isEmpty(mWxAppId) && payBean.getPayType() == PAY_WECHAT){
            try {
                throw new Exception("没配置微信APPID");
            }catch (Exception e){
                e.printStackTrace();
            }
            return;
        }
        mPayType = payBean.getPayType();
        switch (mPayType) {
            case PAY_ALI:
                JvtdAliPay.getInstance().doPay(activity, payBean.getAliParams(), new JvtdAliPay.AlipayResultCallBack() {
                    @Override
                    public void onSuccess() {
                        if (payResultCallBack != null){
                            payResultCallBack.onSuccess(mPayType);
                        }
                    }

                    @Override
                    public void onDealing() {
                        if (payResultCallBack != null){
                            payResultCallBack.onDealing();
                        }
                    }

                    @Override
                    public void onError(int error_code) {
                        int errorCode = ERROR_PAY;
                        switch (error_code) {
                            case JvtdAliPay.ALI_ERROR_NETWORK:
                                errorCode = ERROR_NETWORK;
                                break;
                            case JvtdAliPay.ALI_ERROR_PAY:
                                errorCode = ERROR_PAY;
                                break;
                            case JvtdAliPay.ALI_ERROR_RESULT:
                                errorCode = ERROR_RESULT;
                                break;
                        }
                        if (payResultCallBack != null){
                            payResultCallBack.onError(mPayType,errorCode);
                        }
                    }

                    @Override
                    public void onCancel() {
                        if (payResultCallBack != null){
                            payResultCallBack.onCancel(mPayType);
                        }
                    }
                });
                break;
            case PAY_WECHAT:
                JvtdWxPay.getInstance().init(activity.getApplicationContext(),mWxAppId)
                        .doPay(payBean, new JvtdWxPay.WXPayResultCallBack() {
                            @Override
                            public void onSuccess() {
                                if (payResultCallBack != null){
                                    payResultCallBack.onSuccess(mPayType);
                                }
                            }

                            @Override
                            public void onError(int error_code) {
                                int errorCode = ERROR_PAY;
                                switch (error_code) {
                                    case JvtdWxPay.WX_ERROR_PAY_PARAM:
                                        errorCode = ERROR_PAY_PARAM;
                                        break;
                                    case JvtdWxPay.WX_ERROR_PAY:
                                        errorCode = ERROR_PAY;
                                        break;
                                    case JvtdWxPay.WX_NO_OR_LOW_WX:
                                        errorCode = ERROR_WX_NO_OR_LOW_WX;
                                        break;
                                }
                                if (payResultCallBack != null){
                                    payResultCallBack.onError(mPayType,errorCode);
                                }
                            }

                            @Override
                            public void onCancel() {
                                if (payResultCallBack != null){
                                    payResultCallBack.onCancel(mPayType);
                                }
                            }
                        });
                break;
        }
    }
}
